package com.dsdl.chatapp.module.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dsdl.chatapp.R;
import com.dsdl.chatapp.module.chat.ChatWindowActivity;

import org.json.JSONArray;
import org.json.JSONException;

/**
 * Created by zzg on 2016/8/3.
 * 通讯录
 */
public class ContactsFragment extends Fragment implements AdapterView.OnItemClickListener{
    private static String TAG = "ContactsFragment";
    private ListView lvContacts;
    private ContactsAdapter mContactsAdapter;
    private JSONArray mContacts;

    public static ContactsFragment newInstance(){
        return new ContactsFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_contacts,container,false);
        initView(root);
        return root;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getContactsList();
    }

    private void initView(View view){
        lvContacts = (ListView) view.findViewById(R.id.lv_contacts);
        lvContacts.setOnItemClickListener(this);
    }

    private void getContactsList(){

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        try {
            String friendPhone = (String) mContacts.get(i);
            startActivity(new Intent(getActivity(), ChatWindowActivity.class).putExtra("phone",friendPhone));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    class ContactsAdapter extends BaseAdapter{

        private Context context;
        public ContactsAdapter(Context context){
            this.context = context;
        }

        @Override
        public int getCount() {
            return mContacts.length();
        }

        @Override
        public Object getItem(int i) {
            try {
                return mContacts.get(i);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder= null;
            if (view == null){
                holder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.item_contacts_layout,null);
                holder.tvFriendName = (TextView) view.findViewById(R.id.tv_contacts_username);
                holder.ivPhoto = (ImageView) view.findViewById(R.id.iv_item_contacts_photo);
                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
            }

            try {
                holder.tvFriendName.setText(mContacts.get(i).toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return view;
        }

        class ViewHolder{
            TextView tvFriendName;
            ImageView ivPhoto;
        }
    }
}
